<template>
  <div class="form_box">
    <el-form :label-position="labelPosition" :hide-required-asterisk="true" :model="ruleForm" status-icon :rules="rules"
             ref="ruleForm"
             label-width="100px" class="demo-ruleForm">
      <el-form-item prop="username" label="用户名">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off">
          <i slot="suffix" class="el-input__icon el-icon-date"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off">
          <i slot="suffix" class="el-input__icon el-icon-date"></i>
        </el-input>
      </el-form-item>
      <div>
        <el-button style="width: 100%" type="primary" @click="submitForm('ruleForm')">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
// import {mapActions} from 'vuex'

export default {
  name: "login_form",

  data() {
    let username_check = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        if (this.ruleForm.username !== '') {
          this.$refs.ruleForm.validateField('username');
        }
        callback();
      }
    };
    let password_check = (rule, value, callback) => {
      // console.log(value)
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.password !== '') {
          this.$refs.ruleForm.validateField('password');
        }
        callback();
      }
    };
    return {
      labelPosition: 'left',
      formLabelAlign: {
        name: '',
        region: '',
        type: ''
      },
      ruleForm: {
        username: 'longbin',
        password: '123456',
      },
      rules: {
        username: [
          {validator: username_check, trigger: 'blur'}
        ],
        password: [
          {validator: password_check, trigger: 'blur'}
        ],
      }
    };

  },
  methods: {
    // ...mapActions(['Login']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          // Login()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped lang="scss">
.form_box {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  padding: 10px;
  background-color: #d6d6d7;
  background-image: linear-gradient(to right, white, #014687);
  width: 400px;
  height: 200px;
  border-radius: 10px;

}
</style>
